package com.beautify.qrcode;

import com.beautify.qrcode.model.QrCodeParam;
import com.beautify.qrcode.model.WatermarkVo;
import com.beautify.qrcode.utils.QrCodeUtils;
import com.beautify.qrcode.utils.WatermarkUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * 美化二维码生成工具类
 *
 * @author keia
 * @since 2022/1/13 20:21
 */
@Slf4j
public class ImageQrCodeUtil {

	/**
	 * 创建图片二维码
	 *
	 * @param qrCodeParam
	 * @return
	 * @throws Exception
	 */
	public static QrCodeParam create(QrCodeParam qrCodeParam) throws Exception {
		log.info("美化二维码生成开始");
		boolean[][] s = QrCodeUtils.getQrCode(qrCodeParam.getContent(), qrCodeParam.getErrorCorrect());
		WatermarkVo watermarkVo = WatermarkVo
			.builder()
			.cellArray(s)
			.cellBit(qrCodeParam.getCellBit())
			.height(s.length * qrCodeParam.getCellBit())
			.templateType(qrCodeParam.getTemplateType())
			.build();

		WatermarkUtil watermarkUtil = WatermarkUtil
			.create()
			.watermarkVo(watermarkVo)
			.createCanvas()
			.createEye()
			.createNByN()
			.createOneByOne();

		qrCodeParam.setBeautifyImage(watermarkUtil.outputImage());
		log.info("美化二维码生成结束");
		return qrCodeParam;
	}
}
